package com.orientation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orientation.entity.Class;
import com.orientation.service.ClassService;
import com.orientation.commom.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 班级 前端控制器
 * </p>
 *
 * @author xuandian
 * @since 2022-3-9
 */
@Api(tags = "分配班级")
@RestController
@RequestMapping("/class")
public class ClassController {
    @Resource
    private ClassService classesService;

    @ApiOperation("添加")
    @PostMapping
    public ResponseEntity<Result> insertClass(@RequestBody Class classes){
       classesService.insertClass(classes);
        return ResponseEntity.ok(new Result());
    }
    @ApiOperation("修改")
    @PutMapping
    public boolean update(@RequestBody Class classes) {
        return classesService.updateById(classes);
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable String id) {
        return classesService.removeById(id);
    }

    @ApiOperation("查询单条")
    @GetMapping("/{id}")
    public Class findById(@PathVariable String id) {
        return classesService.getById(id);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public List<Class> findAll() {
        return classesService.list();
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public IPage<Class> findPage(@RequestParam(required = false, defaultValue = "") String name,
                                 @RequestParam(value = "collegeId", required = false) String collegeId,
                                 @RequestParam(value = "majorId", required = false) String majorId,
                                 @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                 @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return classesService.page(new Page<>(pageNum, pageSize), Wrappers.<Class>lambdaQuery().like(StringUtils.isNotBlank(name), Class::getClassName, name)
                .eq(StringUtils.isNotBlank(collegeId), Class::getCollegeId,collegeId)
                .eq(StringUtils.isNotBlank(majorId), Class::getMajorId,majorId));
    }

    @ApiOperation("查询专业下所有班级/废弃不用")
    @GetMapping("/byMajorId")
    public List<Class> findAllByMajorId(String majorId) {
        return classesService.list(Wrappers.<Class>lambdaQuery().eq(StringUtils.isNotBlank(majorId), Class::getMajorId, majorId));
    }

    @ApiOperation("查询专业下所有班级")
    @GetMapping("/byMajorIdOrCollegeId")
    public ResponseEntity<Result> findPage(@RequestParam(value = "collegeId", required = false,defaultValue = "") String collegeId,
                                           @RequestParam(value = "majorId", required = false) String majorId) {
        List<Class> classList = classesService.queryClassesList(collegeId, majorId);
        return ResponseEntity.ok(new Result(classList));
    }

}